<?php
    //require section
    require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_user.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_pager.php';
    $title = 'Admin Panel';

    $id = 0;
    $email = "";
    $first_name = "";
    $last_name = "";
    $address = "";
    $phone = "";
    $action = "Add";
    $action_show = "Thêm";

    if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
        if (!empty($_GET['Id'])) {
            $id = $_GET['Id'];
        }
        $edit_item   = api_user::get_user_by_id($id);
        $id          = $edit_item['user_id'];
        $email       = $edit_item['email'];
        $first_name  = $edit_item['first_name'];
        $last_name   = $edit_item['last_name'];
        $address     = $edit_item['address'];
        $phone       = $edit_item['phone'];
        $action      = "Update";
        $action_show = "Sửa";
    } else if (!empty($_POST)) {
        $id_save = "";
        if (!empty($_POST['Id'])) {
            $id_save = $_POST['Id'];
        }
        $email_save = "";
        if (!empty($_POST['txt_email'])) {
            $email_save = $_POST['txt_email'];
        }
        $first_name_save = "";
        if (!empty($_POST['txt_first_name'])) {
            $first_name_save = $_POST['txt_first_name'];
        }
        $last_name_save = "";
        if (!empty($_POST['txt_last_name'])) {
            $last_name_save = $_POST['txt_last_name'];
        }
        $address_save = "";
        if (!empty($_POST['txt_address'])) {
            $address_save = $_POST['txt_address'];
        }
        $phone_save = "";
        if (!empty($_POST['txt_phone'])) {
            $phone_save = $_POST['txt_phone'];
        }


        if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {
            $error_show = api_user::validate_user_fields($id_save, $email_save, $first_name_save, $last_name_save, $address_save, $phone_save);
            if (empty($error_show)) {
                if (api_user::save_user($id_save, $email_save, $first_name_save, $last_name_save, $address_save, $phone_save)) {
                    $success_info = "Thêm khách hàng &lt;" . $email_save . "&gt; thành công. (mật khẩu mặc định là: 123456)";
                    if ($id_save > 0) {
                        $success_info = "Sửa khách hàng &lt;" . $email_save . "&gt; thành công.";
                    }
                }
            } else {
                $id          = $id_save;
                $email       = $email_save;
                $first_name  = $first_name_save;
                $last_name   = $last_name_save;
                $address     = $address_save;
                $phone       = $phone_save;
                $action      = $_POST['Action'];
                $action_show = "Sửa";
                if ($action === "Add") {
                    $action_show = "Thêm";
                }
            }
        } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
            $id_delete = 0;
            if (!empty($_POST['delete_id'])) {
                $id_delete = $_POST['delete_id'];
            }
            api_user::delete_user($id_delete);
            $success_info = "Xóa khách hàng thành công";
        }
    }



?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
    ?>
</head>
<body>
<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'menu.php';
    ?>

    <section class="ts-body-container uk-panel uk-panel-box ts-padding-large">
        <form action="?" method="POST" id="form_delete">
            <input type="hidden" name="delete_id" value="0"/>
            <input type="hidden" name="Action" value="Delete"/>
        </form>
        <form class="uk-form uk-width-medium-1-3" name="form_data" id="form_data" method="POST"
              action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
            <?php
                if (!empty($error_show)) {
                    ?>
                    <div class="error">Lỗi: <?php echo $error_show; ?></div>
                <?php
                } else if (!empty($success_info)) {
                    ?>
                    <div class="success"><?php echo $success_info; ?></div>
                <?php
                }?>
            <fieldset>
                <legend>Khách Hàng</legend>

                <div class="ui form segment form-background">
                    <input type="hidden" name="Id" value="<?php echo $id; ?>"/>

                    <div class="uk-form-row">
                        <label for="Title">Email <span class="required">*</span></label>
                        <input id="Title" name="txt_email" value="<?php echo $email ?>" placeholder="" type="text">
                    </div>

                    <div class="uk-form-row">
                        <label for="Title">Họ <span class="required">*</span></label>
                        <input id="Title" name="txt_first_name" value="<?php echo $first_name ?>" placeholder=""
                               type="text">
                    </div>

                    <div class="uk-form-row">
                        <label for="Title">Tên <span class="required">*</span></label>
                        <input id="Title" name="txt_last_name" value="<?php echo $last_name ?>" placeholder=""
                               type="text">
                    </div>

                    <div class="uk-form-row">
                        <label for="Title">Địa chỉ <span class="required">*</span></label>
                        <input id="Title" name="txt_address" value="<?php echo $address ?>" placeholder="" type="text">
                    </div>

                    <div class="uk-form-row">
                        <label for="Title">Số điện thoại <span class="required">*</span></label>
                        <input id="Title" name="txt_phone" value="<?php echo $phone ?>" placeholder="" type="text">
                    </div>

                    <div class="uk-form-row">
                        <input type="hidden" name="Action" value="<?php echo $action; ?>">
                        <button class="uk-button uk-button-primary" id="action-button"
                                onclick="submit_data();"><?php echo $action_show; ?></button>
                        <a href="?">Hủy</a>
                    </div>

                </div>
            </fieldset>
        </form>


        <hr/>
        <h3><strong>Danh sách</strong></h3>

        <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
            <thead>
            <tr>
                <th>Email</th>
                <th>Họ</th>
                <th>Tên</th>
                <th>Địa chỉ</th>
                <th>Số điện thoai</th>
                <th>Thao tác</th>
            </tr>
            </thead>
            <tbody>
            <?php
                $current_page = 1;
                $page_size = 10;
                if (!empty($_GET['p'])) {
                    $current_page = $_GET['p'];
                }

                $list_total = api_user::get_all_user();
                $lib_pager = new lib_pager();
                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                $count = 0;
                if (empty($current_page_list) || count($current_page_list) == 0) {
                    echo "<tr><td>Chưa có dữ liệu</td></tr>";
                } else {
                    foreach ($current_page_list as $i => $item) {
                        ?>
                        <tr>
                            <td><?php echo $item['email'] ?></td>
                            <td><?php echo $item['first_name'] ?></td>
                            <td><?php echo $item['last_name'] ?></td>
                            <td><?php echo $item['address'] ?></td>
                            <td><?php echo $item['phone'] ?></td>
                            <td>
                                <a href="?Action=Edit&Id=<?php echo $item['user_id']; ?>">
                                    <i class="uk-icon-edit"></i>
                                </a>
                                <i class="uk-icon-eraser"
                                   onclick="confirmDelete('<?php echo $item['user_id']; ?>')"></i>
                            </td>
                        </tr>
                    <?php
                    }
                } ?>
            <tr align="right" style="color:Black;background-color:#F7F7DE;">
                <td colspan="7">
                    <table>
                        <tbody>
                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                            <td colspan="7">
                                <table>
                                    <tbody>
                                    <tr>
                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                            <td>
                                                                                <span>
                                                                                    <a href="?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                                </span>
                                            </td>
                                        <?php } ?>
                                    </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            </tbody>
        </table>
    </section>

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>
